/**
 * Created by eaTong on 2018/11/29 .
 * Description:
 */
import React from 'react';
import {Container, Header, Content, Button, Text, Form, Item, Input, Label, List, ListItem} from 'native-base';
import BaseComponents from "../components/BaseComponents";
import ajax from "../utils/ajax";

export default class HomeScreen extends BaseComponents {
  static navigationOptions = {
    header: null
  };
  state = {
    form: {
      account: '',
      password: '',
    },
    currentBusiness: []
  };

  async login() {
    const result = await ajax({url: '/api/business/login', data: this.state.form});
    this.setState({currentBusiness: result.business || []});
  }

  async selectBusiness(business) {
    const result = await ajax({
      url: '/api/business/selectBusiness',
      method:'get',
      data: {business_id: business.business_id, business_name: business.business_name}
    });
    this.props.navigation.navigate('EmployeeManage');
  }

  render() {
    const {form, currentBusiness} = this.state;
    return (
      <Container style={{paddingTop: 50}}>
        <Content>
          <Form>
            <Item floatingLabel>
              <Label>账号</Label>
              <Input onChangeText={(text) => this.onChangeForm('account', text)} value={form.account}/>
            </Item>
            <Item floatingLabel>
              <Label>密码</Label>
              <Input onChangeText={(text) => this.onChangeForm('password', text)} value={form.password}/>
            </Item>
          </Form>
          <Button full style={{marginTop: 20}} onPress={() => this.login()}>
            <Text>登陆</Text>
          </Button>

          <List>
            {currentBusiness.map(business => (
              <ListItem key={business.business_id} onPress={() => this.selectBusiness(business)}>
                <Text>{business.business_name}</Text>
              </ListItem>
            ))}
          </List>
        </Content>
      </Container>
    );
  }
}
